<?php

namespace Dddxiu\rules;

use Dddxiu\Layer;
use Dddxiu\traits\Size;

/**
 * 最大值
 */
class Max implements Rule
{
    use Size;

    // flag
    const F = 'max';


    /**
     * @param array $input
     * @param string $field
     * @param Layer $layer
     * @param array $args
     * @return \Closure|bool
     * @throws \Exception
     */
    public static function valid($input, $field, $layer, $args)
    {
        $type = $layer->extra('type', Layer::FIELD_TYPE_STR);
        $max  = $args[1] ?? NULL;
        switch ($type) {
            case Layer::FIELD_TYPE_STR:
                $pass = static::str_len($input[$field], NULL, $max);
                break;
            case Layer::FIELD_TYPE_NUM:
                $pass = static::num_size($input[$field], NULL, $max);
                break;
            case Layer::FIELD_TYPE_DATE:
                $pass = static::date_span($input[$field], NULL, $max);
                break;
        }
        if (isset($pass) and $pass) {
            return $layer::next();
        }
        return false;
    }
}